package com.angel.activity;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.angel.adapter.MyAdapter;
import com.angel.entity.SearchActivityList;
import com.angel.entity.Task;
import com.angel.port.IActivity;
import com.angel.service.TaskService;
import com.angel.util.BaseUtil;
import com.angel.util.StringUtil;
import com.google.gson.reflect.TypeToken;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
/**搜索活动*/
public class SearchActivity extends Activity implements IActivity {
	
	private ListView listView = null;
	/** 自定义Adapter */
	private MyAdapter searchAdapter = null;
	private MyAdapter myAdapter = null;
	private Context context;
//	private List<SearchActivity> list;
	private List<String> activityIdList;//用于保存活动Id
	private SearchActivityList searchActivityList;
	private HashMap<String,String> map;
	private EditText editSearch;
	private ProgressDialog progressDialog;
	private String searchTxt;//搜索内容
	private ViewGroup viewFoot;
	private TextView moreTxt;//更多
	private LinearLayout layoutRefresh;//显示加载布局
	private String maxId="0";//当前所在页面数
	private boolean isShow;//判断是否显示加载还是更多
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_activity);
		 MobclickAgent.onError(this);//自动捕获异常退出（FC ）
		context=SearchActivity.this;
		progressDialog=new ProgressDialog(context);
		progressDialog.setMessage("加载中,请耐心等候...");
		
		listView = (ListView) findViewById(R.id.search_ListView);
		editSearch=(EditText)findViewById(R.id.search_edit);
		context=SearchActivity.this;
		
		viewFoot=(ViewGroup)getLayoutInflater().inflate(R.layout.more, null);
		/*添加底部，更多*/
		listView.addFooterView(viewFoot, null, true);
		listView.setOnItemClickListener(new ListItemOnClickListener());
		moreTxt=(TextView)viewFoot.findViewById(R.id.more_txt_id);
		layoutRefresh=(LinearLayout)viewFoot.findViewById(R.id.refresh_layout_id);
		viewFoot.setOnClickListener(new ClickMoreListener());
	}
	 class ListItemOnClickListener implements OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			 
			String activityId=activityIdList.get(arg2);
			Intent intent = new Intent();
			intent.putExtra("activityId", activityId);//传递活动Id
			intent.setClass(SearchActivity.this, ActivityDetail.class);
			startActivity(intent); 
		}
	}
	/*点击加载更多*/
	class ClickMoreListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			isShow=true;//显示加载
			isShowLoad();
			addMore();
		}
	}
	/*选择更多*/
	public void addMore(){
		map=new HashMap<String,String>(2);
//		map.put("userId", UserInfoUtil.getValue(context, "userId", "1"));
		map.put("searchContent", searchTxt);
		map.put("maxId", String.valueOf(maxId));//发送当前页数
		BaseUtil.startService(Task.SEARCH_ACTIVITY, this, map);
		progressDialog.show();
	}
	/*显示底部加载*/
	public void isShowLoad(){
		moreTxt.setVisibility(isShow?View.GONE:View.VISIBLE);
		layoutRefresh.setVisibility(isShow?View.VISIBLE:View.GONE);
	}
	@Override
	public void init() {
	}
	@Override
	public void refresh(Object... params) {
		String isSucess=BaseUtil.getValueFromJSON((String)params[0]);
		
		 if(isSucess.equals("2")){
			 BaseUtil.makeToast(SearchActivity.this, "对不起,网络连接超时,请稍后重试...");
			 return ;
		}
		//关闭加载视图
		isShow=false;
		isShowLoad();
		Type type=new TypeToken<SearchActivityList>(){}.getType();
		searchActivityList=(SearchActivityList)BaseUtil.getEntityFromJson((String)params[0], type);
		
		if(searchActivityList.getIsSuccess().equals("1")){//表示获取到的内容不为空
			//将活动id添加到全局数组中
			for(int i=0;i<searchActivityList.getResult().size();i++){
				activityIdList.add(searchActivityList.getResult().get(i).getActivityId());
			}
			
			if(maxId.equals("0")){//如果是第一次加载,则生成myAdapter实例
				myAdapter = new MyAdapter(context,searchActivityList.getResult());
				listView.setAdapter(myAdapter);
			}else{
				myAdapter.refresh(searchActivityList.getResult());
				listView.setSelection(listView.getCount()-11);
			}
			//获取最后一条记录的Id
			maxId=searchActivityList.getResult().get(searchActivityList.getResult().size()-1).getActivityId();
		}else{
			BaseUtil.makeToast(context, "已是最新活动列表或没相关活动");
		}
		progressDialog.dismiss();
	}
	public void searchCheck(View v){
		
		searchTxt=editSearch.getText().toString();
		if(StringUtil.isBlank(searchTxt)){//判断搜索内容是否为空
			BaseUtil.makeToast(this, "请输入您要搜索的内容");
		}else{
			postSearch(searchTxt);
		}
	}
	/*根据搜索内容请求搜索*/
	public void postSearch(String searchTxt){
		activityIdList=new ArrayList<String>(5);//初始化id数组
		maxId="0";
		map=new HashMap<String,String>(2);
		map.put("searchContent", searchTxt);
		map.put("maxId", "0");//发送当前页数，默认为1
		BaseUtil.startService(Task.SEARCH_ACTIVITY, this, map);
		progressDialog.show();
		
		MobclickAgent.onEvent(this, "Search", map);//发送搜索内容到友盟
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			BaseUtil.showExitDialog(this);
		}
		return super.onKeyDown(keyCode, event);
	}
	/*刷新搜索内容*/
	public void searchRefresh(View v){
		postSearch(searchTxt);
		progressDialog.show();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		 MobclickAgent.onResume(this);
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		 MobclickAgent.onPause(this);
	}
}
